package com.tuanzi.loan.web.bean.receipt.product;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.receipt.product.ReceiptProductCondition;
import com.tuanzi.loan.business.entity.receipt.product.ReceiptProductInfo;
import com.tuanzi.loan.business.enums.RegionTreeEnum;
import com.tuanzi.loan.business.service.common.CommRegionNewService;
import com.tuanzi.loan.business.service.receipt.product.ReceiptProductConditionService;
import com.tuanzi.loan.business.service.receipt.product.ReceiptProductInfoService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.server.loan_base.constant.LoanReceiptProductStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Objects;

import static com.tuanzi.loan.business.constants.DictionaryCode.RECEIPT_SUBACCOUNT_ORDER_ALLOT;
import static com.tuanzi.loan.business.constants.DictionaryCode.WEEKS;


@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ReceiptProductApproveBean extends JSFInfoBean<ReceiptProductInfo> {

    private Long productId;
    private boolean buttonView =true;
    private ReceiptProductCondition receiptProductCondition;
    private TreeNode areaRegion;
    private TreeNode[] selectedRegionNodes;

    @Autowired
    private ReceiptProductInfoService receiptProductInfoService;
    @Autowired
    private ReceiptProductConditionService receiptProductConditionService;
    @Autowired
    private CommRegionNewService commRegionNewService;

    @PostConstruct
    private void init(){
        String id = getParamFromRequest("id");
        if(StringUtils.isNotBlank(id)){
            productId = Long.parseLong(id);
            $ = receiptProductInfoService.findOne(productId);
            receiptProductCondition = receiptProductConditionService.findReceiptProductConditionByProductInfo($);
            if($.getStatus() != LoanReceiptProductStatus.AUDIT){
                buttonView = false;
            }
        }

        if(Objects.isNull($)){
            $ = new ReceiptProductInfo();
        }
        if(Objects.isNull(receiptProductCondition)){
            receiptProductCondition = new ReceiptProductCondition();
        }

        areaRegion = commRegionNewService.generateTreeNode(receiptProductCondition.getProvince()+receiptProductCondition.getCity(), RegionTreeEnum.RECEIPT);

    }


    public void onCommit(){
        buttonView = false;
        receiptProductInfoService.updateProductInfoAndConditionByStatus($, receiptProductCondition,LoanReceiptProductStatus.AUDIT_AGREE);
        addSuccessfullyMessage();
    }

    public void onReject(){
        if(StringUtils.isBlank($.getRejectReason())){
            addErrorMessage("请填写拒绝原因");
            return;
        }
        buttonView = false;
        receiptProductInfoService.updateProductInfoAndConditionByStatus($, receiptProductCondition,LoanReceiptProductStatus.AUDIT_REJECT);
        addSuccessfullyMessage();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(WEEKS, RECEIPT_SUBACCOUNT_ORDER_ALLOT);
    }
}
